package djboom.materials.display.devices {
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.state.Font;

	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaSimpleButton;
	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.events.Event;
	import flash.text.AntiAliasType;
	import flash.text.StyleSheet;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * Весы.
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 12.05.2010
	 * 
	 */

	public class Scales extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const LIMIT_LOAD_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 11, 0x33393f, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _limitLoadField:CasaTextField;
		private var _weightField:CasaTextField;

		private var _scaleStageBitmap:CasaBitmap;
		private var _stageBitmap:CasaBitmap;
		private var _buttonResetBitmap:CasaBitmap;
		private var _buttonTaraBitmap:CasaBitmap;
		private var _buttonReset:CasaSimpleButton;
		private var _buttonTara:CasaSimpleButton;
		private var _initWeight:String;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Scales(initWeight:String, limitLoad:String = null) {
			super();

			_initWeight = initWeight;
			
			_scaleStageBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.SCALE_STAGE));
			_scaleStageBitmap.x = 22;
			addChild(_scaleStageBitmap);
			
			_stageBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.SCALE));
			_stageBitmap.y = 33;
			addChild(_stageBitmap);
			
			_buttonResetBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.SCALE_BUTTON_RESET));
			
			_buttonReset = new CasaSimpleButton(_buttonResetBitmap, _buttonResetBitmap, _buttonResetBitmap, _buttonResetBitmap);
			_buttonReset.x = 179;			_buttonReset.y = 46;
			_buttonReset.tabEnabled = false;
			addChild(_buttonReset);
			
			_buttonTaraBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.SCALE_BUTTON_TARA));
			
			_buttonTara = new CasaSimpleButton(_buttonTaraBitmap, _buttonTaraBitmap, _buttonTaraBitmap, _buttonTaraBitmap);
			_buttonTara.x = 179;
			_buttonTara.y = 83;
			_buttonTara.tabEnabled = false;
			addChild(_buttonTara);
			
			var style:StyleSheet = new StyleSheet();
			style.setStyle("p", {fontFamily: Font.CRYSTAL, fontSize: "25", color: "#1d2528", textAlign: "right"});
			style.setStyle(".16px", {fontSize: "16"});
            
			_weightField = new CasaTextField();
			_weightField.embedFonts = true;
			_weightField.selectable = false;
			_weightField.styleSheet = style;
			_weightField.width = 154;
			_weightField.autoSize = TextFieldAutoSize.RIGHT;
			_weightField.antiAliasType = AntiAliasType.ADVANCED;
			_weightField.htmlText = '<p>' + _initWeight + ' <span class="16px">г</span></p>';
			_weightField.mouseEnabled = false;			_weightField.y = 64;
			addChild(_weightField);
			
			if (limitLoad != null) {
				_limitLoadField = new CasaTextField();
				_limitLoadField.embedFonts = true;
				_limitLoadField.selectable = false;
				_limitLoadField.defaultTextFormat = LIMIT_LOAD_TFORMAT;
				_limitLoadField.autoSize = TextFieldAutoSize.LEFT;
				_limitLoadField.antiAliasType = AntiAliasType.ADVANCED;
				_limitLoadField.text = limitLoad;
				_limitLoadField.mouseEnabled = false;
				_limitLoadField.x = 22;				_limitLoadField.y = 98;
				addChild(_limitLoadField);
			}
			
			addEventListener(Event.REMOVED_FROM_STAGE, clearContent);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clearContent(event:Event):void {
			_weightField.destroy();
			_weightField = null;
			
			if (_limitLoadField != null) {
				_limitLoadField.destroy();
				_limitLoadField = null;
			}
			
			_scaleStageBitmap.destroy();
			_scaleStageBitmap = null;
			
			_stageBitmap.destroy();
			_stageBitmap = null;
            
			_buttonResetBitmap.destroy();
			_buttonResetBitmap = null;
            
			_buttonTaraBitmap.destroy();
			_buttonTaraBitmap = null;
            
			_buttonReset.destroy();
			_buttonReset = null;
            
			_buttonTara.destroy();
			_buttonTara = null;
		}

		
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function resetButton():void {
			_buttonReset.useHandCursor = false;			_buttonTara.useHandCursor = false;
		}

		public function setupWeight(weight:String):void {
			_weightField.htmlText = '<p>' + weight + ' <span class="16px">г</span></p>';
		}

		public function animateWeightDown():void {
			TweenLite.to(_scaleStageBitmap, 0.5, {y: 10, ease: Cubic.easeOut});
		}

		public function clearScale():void {
			TweenLite.to(_scaleStageBitmap, 0.5, {y: 0, ease: Cubic.easeIn});
			_weightField.htmlText = '<p>' + _initWeight + ' <span class="16px">г</span></p>';
		}

		public function animateWeightDownRelative(i:int):void {
			TweenLite.to(_scaleStageBitmap, 0.5, {y: String(i), ease: Cubic.easeOut});
		}
        	
			// ----------------------------------------------------------------------------
	        // Публичные свойства.
	        // ----------------------------------------------------------------------------
	}
}
